package com.wd.lobby;

import com.wd.command.BaseCommandObject;
import com.wd.command.Command;
import com.wd.command.CommandSet;
import com.wd.command.Protocol;
import com.wd.config.Config;
import com.wd.entity.server.IServerEntity;
import com.wd.entity.user.UserInfoEntity;
import com.wd.lobby.mgr.UserMgr;
import com.wd.nio.GameData;
import com.wd.nio.SessionMgr;
import com.wd.nio.SessionObject;
import com.wd.nio.handler.IoHandlerAdapter;
import com.wd.threadpool.ThreadPool;

/**
 * @author long
 * @date 2013-5-9 下午3:53:48
 * @qq 108232706
 * @description LobbySessionMgr
 */
final public class LobbySessionMgr extends SessionMgr {

	private final IServerEntity Server = new ServerEntity();

	public LobbySessionMgr(IoHandlerAdapter _handler) {
		super(_handler);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected BaseCommandObject getFreeCommand() {
		return ThreadPool.getFreeCommand();
	}

	@Override
	public void OnCheck(SessionObject sessionObj, BaseCommandObject command) {
		// 玩家状态为1,在等待外面调用检查，
		try {
			if (command == null) {
				return;
			}
			if (command.getCmd() != Protocol.UserLoginCmd)// 第一个包一定是关于1000登陆
			{
				// 命令如果不push到线程池，必须手动回收
				ThreadPool.freeUsedCommand(command);
				return;
			}
			sessionObj.cmdStatus[1] = 1;
			ThreadPool.Push(command);
		} catch (Exception e) {
			logger.info("OnCheck Push command failure!", e);
		}

	}

	@Override
	public void OnCheckSuss(SessionObject sessionObj, BaseCommandObject command) {
		try {
			if (command != null) {
				// 命令如果不push到线程池，必须手动回收
				ThreadPool.freeUsedCommand(command);
			}
		} catch (Exception e) {
			logger.info("OnConnect Push command failure!", e);
		}

	}

	@Override
	public void OnConnect(SessionObject sessionObj, BaseCommandObject command) {
		try {
			// 停止外面，该玩家已经验证通过，等待置为Ready状态
			sessionObj.setStatus(GameData.READYGAME);
			if (command == null) {
				return;
			} else {
				// 命令如果不push到线程池，必须手动回收
				ThreadPool.freeUsedCommand(command);
			}

		} catch (Exception e) {
			logger.info("OnConnect Push command failure!", e);
		}

	}

	@Override
	public void OnDisconncet(SessionObject sessionObj, BaseCommandObject command) {
		// 告诉外界，这个人离开啦
		try {
			if (command != null) {
				// 命令如果不push到线程池，必须手动回收
				ThreadPool.freeUsedCommand(command);
			}
		} catch (Exception e) {
			logger.info("OnConnect Push command failure!", e);
		}

	}

	@Override
	public void OnWaitCheck(SessionObject sessionObj, BaseCommandObject command) {
		try {
			if (command != null) {
				// 命令如果不push到线程池，必须手动回收
				ThreadPool.freeUsedCommand(command);
			}
		} catch (Exception e) {
			logger.info("OnWaitCheck Push command failure!", e);
		}

	}

	@Override
	protected boolean startThreadPool() {
		try {
			return ThreadPool.Start(this, Server.CmdThreadCount, Server.CmdQueueCount);
		} catch (Throwable e) {
			e.printStackTrace();
			return false;

		}
	}

	@Override
	protected UserInfoEntity createUserInfoEntity() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void threadPoolExit() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void threadPoolPause() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void threadPoolStop() {
	}

	@Override
	protected void clearAllUserList() {
		UserMgr.clearAllUserList();
	}

	@Override
	protected boolean initCommandSet() {
		return CommandSet.load();
	}

	@Override
	public Command getCommand(short cmd) {
		return CommandSet.getCommand(cmd);
	}

	@Override
	protected IServerEntity createNewServer() {
		return Server;
	}

	@Override
	protected boolean initServer() {
		Server.Port = Config.getInt("server.port");
		Server.SessionMaxCount = Config.getInt("server.sessionMaxCount");
		Server.CmdThreadCount = Config.getInt("server.cmdThreadCount");
		Server.CmdQueueCount = Config.getInt("server.cmdQueueCount");
		//从数据库读取
		return true;
	}

	@Override
	public void OnCommand(SessionObject sessionObj, BaseCommandObject command) {
		try {
			if (command == null) {
				return;
			}
			ThreadPool.Push(command);
		} catch (Exception e) {
			logger.info("OnCommand Push command failure!", e);
		}

	}

}
